<?php
class Model_ModuleAdmin extends Table_ModuleAdmin {
    private $moduleList = NULL;
    private $moduleDirs = array();
    private $rootDir = array();
    function Model_ModuleAdmin($params = NULL) {
        parent::__construct($params);
        $this->rootDir = FLEA::getAppInf('rootDir');
        $this->updateList();
    }
    // 更新模块列表
    function updateList() {
        // 硬盘上存在的模块
        $modules = array();
        // 系统配置中默认的模块目录
        $moduleDirs = FLEA::getAppInf('moduleDirs');
        foreach($moduleDirs as $dir) {
            $result = $this->scan($dir);
            foreach($result as $value) {
                array_push($modules, $value);
            }
        }
        $this->moduleList = $this->findAll(NULL, 'mid ASC', NULL, array('name', 'path'));
        foreach($modules as &$value) {
            $value['path'] = array_pop(explode($this->rootDir['absolute'], $value['path']));
            $value = serialize($value);
        }
        $modulesInDb = $this->moduleList;
        foreach($modulesInDb as &$value) {
            $value = serialize($value);
        }
        $toAdd = array_diff($modules, $modulesInDb);
        $toDelete = array_diff($modulesInDb, $modules);
        // 移除不存在硬盘上的模块
        foreach($toDelete as &$value) {
            $value = unserialize($value);
            $this->removeByField('path', $value['path']);
        }
        // 添加新模块
        foreach($toAdd as &$value) {
            $value = unserialize($value);
            $info = require($this->rootDir['absolute'] .$value['path'] .'/info.php');
            $value = array(
                'name' => $info['name'],
                'description' => isset($info['description'])?$info['description']:'', 
                'group' => isset($info['group'])?$info['group']:'other',
                'path' => $value['path'],
                'core' => False,
                'enabled' => False,
            );
            $this->save($value);
        }
    }
    // 将模块目录加入class path
    function addModulePath() {
        $modulesEabled = $this->findAllByField('enabled', 1);
        $modulesCore = $this->findAllByField('core', 1);
        $modules = array_merge($modulesEabled, $modulesCore);
        foreach($modules as $module) {
            FLEA::import($this->rootDir['absolute']. $module['path']);
        }
    }
    /**
     * 启用模块
     * @param int $mid 模块id
     * @return mixed 若成功则返回模块编号，失败返回false
     */
    function enableModule($mid) {
        $mid = (int) $mid;
        $module = $this->findAllByField('mid', $mid);
        if(!$module) {
            return false;
        }
        $module = $module[0];
        $module['enable'] = 1;
        if($this->update($module)) {
            return $module['mid'];
        } else {
            return false;
        }
    }
    /**
     * scan module dir
     * @see Model_Theme::scanThemeDir
     */
    function scan($dir) {
        $dirs = array();
        if(!is_dir($dir)) {
            return false;
        }
        $entries = scandir($dir);
        if(in_array('info.php', $entries)) {
            $info = require_once($dir .'/' .'info.php');
            $path = $dir;
            return array('name' => $info['name'], 'path' => $path);
        } else {
            foreach($entries as $entry) {
                if($entry == '.' OR $entry == '..') {
                    continue;
                }
                $result = $this->scan($dir. '/'. $entry);
                // 判断返回的是一个模块目录或者是多个模块目录，进行不同处理
                if($result) {
                    if(array_key_exists('name', $result)) {
                        array_push($dirs, $result);
                    } else {
                        foreach($result as $value) {
                            array_push($dirs, $value);
                        }
                    }
                }
            }
        }
        return $dirs;
    }
}
